/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.Category;
import com.blog.city.checkthedep.sessionbeans.CategoryFacadeLocal;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author Breath
 */
@ManagedBean
@RequestScoped
public class CategoryBean {

    private Category selectedCategory = new Category();
    private String searchByCategory = "";
    private List<Article> articlesByCategory;
    @EJB
    private CategoryFacadeLocal categoryFac;
    private List<Category> categories;
    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;
    
    
    /**
     * 
     */
    @PostConstruct
    public void retrieveCategories() {
        this.setCategories(categoryFac.findAll());
    }

    /**
     * 
     * @return
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * 
     * @param loginBean
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }


    /**
     * 
     * @return
     */
    public CategoryFacadeLocal getCategoryFac() {
        return categoryFac;
    }

    /**
     * 
     * @param categoryFac
     */
    public void setCategoryFac(CategoryFacadeLocal categoryFac) {
        this.categoryFac = categoryFac;
    }

    /**
     * 
     * @return
     */
    public List<Category> getCategories() {
        return categories;
    }

    /**
     * 
     * @param categories
     */
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    /**
     * 
     * @return
     */
    public String getSearchByCategory() {
        return searchByCategory;
    }

    /**
     * 
     * @param searchByCategory
     */
    public void setSearchByCategory(String searchByCategory) {
        this.searchByCategory = searchByCategory;
    }

    /**
     * 
     * @return
     */
    public Category getSelectedCategory() {
        return selectedCategory;
    }

    /**
     * 
     * @param selectedCategory
     */
    public void setSelectedCategory(Category selectedCategory) {
        this.selectedCategory = selectedCategory;
    }

    /**
     * 
     * @param id
     */
    public void retrieveSelectedCategory(Integer id) {
        this.selectedCategory = categoryFac.find(id);

    }

    /**
     * 
     */
    public void persistSelectedCategory() {
        if (selectedCategory != null) {
            categoryFac.edit(selectedCategory);
        }
    }

    /**
     * 
     * @param id
     */
    public void deleteSelectedCategory(Integer id) {
        this.retrieveSelectedCategory(id);
        if (selectedCategory != null) {
            categoryFac.remove(selectedCategory);
        }
        this.retrieveCategories();
    }

    /**
     * 
     */
    public void persistCategory() {
        if (selectedCategory != null) {
            categoryFac.create(selectedCategory);
        }
    }

    /**
     * 
     */
    public void editCategory() {
        if (selectedCategory != null) {
            categoryFac.edit(selectedCategory);
        }
    }

    /**
     * 
     */
    public void deleteCategory() {
        if (selectedCategory != null) {
            categoryFac.remove(selectedCategory);
        }
    }

    /**
     * 
     * @return
     */
    public List<Article> getArticlesByCategory() {
        return articlesByCategory;
    }

    /**
     * 
     * @param articlesByCategory
     */
    public void setArticlesByCategory(List<Article> articlesByCategory) {
        this.articlesByCategory = articlesByCategory;
    }
    
    /**
     * 
     */
    public void findByCategory() {
        this.setArticlesByCategory(categoryFac.findArticleByCategory(categoryFac.findCategoryByName(searchByCategory)));
    }
}
